<?php

namespace IMT\PluginBundle\Services;

use Symfony\Component\HttpFoundation\Request;

use Symfony\Bundle\TwigBundle\TwigEngine;

use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\Security\Acl\Model\AclProviderInterface;
use Symfony\Component\Security\Acl\Domain\ObjectIdentity;
use Symfony\Component\Security\Acl\Domain\UserSecurityIdentity;
use Symfony\Component\Security\Acl\Permission\MaskBuilder;
use Doctrine\ORM\EntityManager;
use IMT\PluginBundle\Models\PluginInterface;
use IMT\PluginBundle\Models\PluginManager as BasePluginManager;
use Symfony\Component\Validator\Constraint;

class PluginManager extends BasePluginManager
{
    protected $em;
    protected $class;
    protected $repository;

    /**
     * Constructor.
     *
     * @param AclProvider			  $aclProvider
     * @param SecurityContext		  $securityContext
     * @param EntityManager           $em
     * @param string                  $class
     */
    public function __construct(
    		AclProviderInterface $aclProvider, 
    		SecurityContext $securityContext, 
    		Request $request,
    		TwigEngine $templating,
    		EntityManager $em, 
    		$class
    )
    {
        parent::__construct($aclProvider, $securityContext, $request, $templating);

        $this->em = $em;
        $this->repository = $em->getRepository($class);

        $metadata = $em->getClassMetadata($class);
        $this->class = $metadata->name;
    }
    
    
    /**
     * {@inheritDoc}
     */
    public function updatePlugin(PluginInterface $plugin, $autoFlush = true)
    {
    	
    }
    
    /**
     * {@inheritDoc}
     */
    public function deletePlugin(PluginInterface $plugin)
    {
    	$this->em->remove($plugin);
    	$this->em->flush();
    }
    
    /**
     * {@inheritDoc}
     */
    public function getClass()
    {
    	return $this->class;
    }
    
    /**
     * {@inheritDoc}
     */
    public function findPluginBy(array $criteria)
    {
    	return $this->repository->findOneBy($criteria);
    }
    
}